home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 August: Tool Chest / Dev.CD Aug 00 TC Disk 2.toast / pc / sample code / human interface toolbox / packagetool / sample package / htmlsample sources / aboutbox.c next >
Encoding:
C/C++ Source or Header  |  2000-06-23  |  7.5 KB  |  203 lines

  1. /*
  2.     File: AboutBox.c
  3.     
  4.     Description:
  5.         This file contains the routines used to manage the about box window
  6.     displayed when the user chooses 'About HTMLSample...' from the file menu.
  7.     
  8.     HTMLSample is an application illustrating how to use the new
  9.     HTMLRenderingLib services found in Mac OS 9. HTMLRenderingLib
  10.     is Apple's light-weight HTML rendering engine capable of
  11.     displaying HTML files.
  12.  
  13.     Copyright:
  14.         © Copyright 1999 Apple Computer, Inc. All rights reserved.
  15.     
  16.     Disclaimer:
  17.         IMPORTANT:  This Apple software is supplied to you by Apple Computer, Inc.
  18.         ("Apple") in consideration of your agreement to the following terms, and your
  19.         use, installation, modification or redistribution of this Apple software
  20.         constitutes acceptance of these terms.  If you do not agree with these terms,
  21.         please do not use, install, modify or redistribute this Apple software.
  22.  
  23.         In consideration of your agreement to abide by the following terms, and subject
  24.         to these terms, Apple grants you a personal, non-exclusive license, under Apple’s
  25.         copyrights in this original Apple software (the "Apple Software"), to use,
  26.         reproduce, modify and redistribute the Apple Software, with or without
  27.         modifications, in source and/or binary forms; provided that if you redistribute
  28.         the Apple Software in its entirety and without modifications, you must retain
  29.         this notice and the following text and disclaimers in all such redistributions of
  30.         the Apple Software.  Neither the name, trademarks, service marks or logos of
  31.         Apple Computer, Inc. may be used to endorse or promote products derived from the
  32.         Apple Software without specific prior written permission from Apple.  Except as
  33.         expressly stated in this notice, no other rights or licenses, express or implied,
  34.         are granted by Apple herein, including but not limited to any patent rights that
  35.         may be infringed by your derivative works or by other works in which the Apple
  36.         Software may be incorporated.
  37.  
  38.         The Apple Software is provided by Apple on an "AS IS" basis.  APPLE MAKES NO
  39.         WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED
  40.         WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  41.         PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN
  42.         COMBINATION WITH YOUR PRODUCTS.
  43.  
  44.         IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR
  45.         CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  46.         GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  47.         ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION
  48.         OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF CONTRACT, TORT
  49.         (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN
  50.         ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  51.  
  52.     Change History (most recent first):
  53.         Wed, Dec 22, 1999 -- created
  54. */
  55.  
  56.  
  57. #include "AboutBox.h"
  58.  
  59. #include <HTMLRendering.h>
  60. #include <Resources.h>
  61. #include <StdDef.h>
  62. #include <string.h>
  63. #include <Sound.h>
  64.  
  65.  
  66. enum {
  67.     kAboutBoxWindowID = 129, /* WIND resource ID for the about box. */
  68.     kMaxAboutBoxHeight = 380 /* maximum about box window height */
  69. };
  70.  
  71.     /* resource type and ID for the about box's contents */
  72. enum {
  73.     kHTMLResourceType = 'HTML',
  74.     kAboutBoxHTMLID = 128
  75. };
  76.  
  77.     /* variables used by the about box.  there is only
  78.         one about box, so these are allocated as globals */
  79. WindowPtr gAboutBox = NULL;
  80. HRReference gAboutRenderer = NULL;
  81.  
  82.  
  83. /* IsAboutBox returns true if the window pointer
  84.     in the aboutBox parameter is not NULL and
  85.     points to the about box. */
  86. Boolean IsAboutBox(WindowPtr aboutBox) {
  87.     return (aboutBox == gAboutBox && aboutBox != NULL);
  88. }
  89.  
  90.  
  91. /* OpenAboutBox opens the about box window and returns
  92.     a pointer to the window in *aboutBox.  There can only
  93.     be one about box open at a time, so if the about box is
  94.     already open, then this routine brings it to the front
  95.     by calling SelectWindow before returning a pointer to
  96.     it. */
  97. OSStatus OpenAboutBox(WindowPtr *aboutBox) {
  98.     OSStatus err;
  99.     if (gAboutBox != NULL) {
  100.             /* already showing??? bring it to the front. */
  101.         SelectWindow(gAboutBox);
  102.     } else {
  103.         Point theSize;
  104.         Handle text;
  105.         gAboutBox = NULL;
  106.         gAboutRenderer = NULL;
  107.             /* get the window */
  108.         gAboutBox = GetNewCWindow(kAboutBoxWindowID, NULL, (WindowPtr)(-1));
  109.         if (gAboutBox == NULL) { err = resNotFound; goto bail; }
  110.             /* allocate a new rendering object */
  111.         err = HRNewReference(&gAboutRenderer, kHRRendererHTML32Type, gAboutBox);
  112.         if (err != noErr) goto bail;
  113.             /* we don't have a grow box in this window */
  114.         err = HRSetGrowboxCutout(gAboutRenderer, false);
  115.         if (err != noErr) goto bail;
  116.             /* we only want a vertical scroll bar */
  117.         err = HRSetScrollbarState(gAboutRenderer, eHRScrollbarOff, eHRScrollbarAuto);
  118.         if (err != noErr) goto bail;
  119.             /* set the bounds for the rendering object */
  120.         SetPort(gAboutBox);
  121.         err = HRSetRenderingRect(gAboutRenderer, &gAboutBox->portRect);
  122.         if (err != noErr) goto bail;
  123.             /* get the HTML data we want to draw in the window */
  124.         text = GetResource(kHTMLResourceType, kAboutBoxHTMLID);
  125.         if (text == NULL) { err = resNotFound; goto bail; }
  126.             /* put the data into the rendering object */
  127.         MoveHHi(text);
  128.         HLock(text);
  129.         err = HRGoToPtr(gAboutRenderer, *text, GetHandleSize(text), false, false);
  130.         HUnlock(text);
  131.         if (err != noErr) goto bail;
  132.             /* find out the 'best' rectangle for displaying the data */
  133.         err = HRGetRenderedImageSize(gAboutRenderer, &theSize);
  134.         if (err != noErr) goto bail;
  135.             /* adjust the window's size, constraining it by the
  136.             maximum size */
  137.         if (theSize.v > kMaxAboutBoxHeight) theSize.v = kMaxAboutBoxHeight;
  138.         SizeWindow(gAboutBox, theSize.h+16, theSize.v, false);
  139.         SetPort(gAboutBox);
  140.             /* adjust the rendering object's size to the window's size */
  141.         err = HRSetRenderingRect(gAboutRenderer, &gAboutBox->portRect);
  142.         if (err != noErr) goto bail;
  143.             /* show the window */
  144.         ShowWindow(gAboutBox);
  145.     }
  146.         /* done, return the window pointer */
  147.     *aboutBox = gAboutBox;
  148.     return noErr;
  149. bail:
  150.     if (gAboutRenderer != NULL) { HRDisposeReference(gAboutRenderer); gAboutRenderer = NULL; }
  151.     if (gAboutBox) { DisposeWindow(gAboutBox); gAboutBox = NULL; }
  152.     return err;
  153. }
  154.  
  155.  
  156. /* AboutBoxCloseWindow closes the about box window. 
  157.     this routine deallocates any structures allocated
  158.     by the OpenAboutBox. */
  159. void AboutBoxCloseWindow(WindowPtr aboutBox) {
  160.     if (IsAboutBox(aboutBox)) { 
  161.         HRDisposeReference(gAboutRenderer);
  162.         gAboutRenderer = NULL;
  163.         DisposeWindow(gAboutBox);
  164.         gAboutBox = NULL;
  165.     }
  166. }
  167.  
  168.  
  169. /* EnsureAboutBoxIsClosed closes the about box window
  170.     if it is open.  If it is not open then this routine does
  171.     nothing. */
  172. void EnsureAboutBoxIsClosed(void) {
  173.     if (gAboutBox != NULL) 
  174.         AboutBoxCloseWindow(gAboutBox);
  175. }
  176.  
  177.  
  178. /* AboutBoxUpdate should be called for update events
  179.     directed at the about box window.  It calls
  180.     BeginUpdate and EndUpdate and does all of the
  181.     drawing required to refresh the about box window. */
  182. void AboutBoxUpdate(WindowPtr aboutBox) {
  183.     if (IsAboutBox(aboutBox)) {
  184.         SetPort(gAboutBox);
  185.         BeginUpdate(gAboutBox);
  186.         HRDraw(gAboutRenderer, gAboutBox->visRgn);
  187.         EndUpdate(gAboutBox);
  188.     }
  189. }
  190.  
  191.  
  192. /* AboutBoxActivate should be called for activate events
  193.     directed at the about box window. */
  194. void AboutBoxActivate(WindowPtr aboutBox, Boolean activate) {
  195.     if (IsAboutBox(aboutBox)) {
  196.         SetPort(gAboutBox);
  197.         if (activate)
  198.             HRActivate(gAboutRenderer);
  199.         else HRDeactivate(gAboutRenderer);
  200.     }
  201. }
  202.  
  203.